/*
 * MessageFilter.java    0.0.1    08/dic/2009
 * 
 * Copyright (c) 2009 mentalsmash.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * The use of the Apache License does not indicate that this project is
 * affiliated with the Apache Software Foundation.
 */ 
package org.mentalsmash.tazio.transparency.interfaces;

import org.mentalsmash.tazio.transparency.commons.messages.TazioMessage;

/**
 * A Message Filter is an entity which works when a message is received. It is used when 
 * a message sent with the at-most-once semantics has been received and checks if the 
 * arrived message had already been received. 
 * <p>
 * When the arrival of a message is checked, there are three main scenarios:
 * <ul>
 * <li>
 * the message had already been received and its answer calculated, this means that the 
 * answer had not been brought to the sender of the original message, probably due to some 
 * network problem, thus instead of dispatching the message into the system a second time, the 
 * stored answer is retrieved and sent again to the sender.
 * </li>
 * <li>
 * the message had already been received but the answer is being calculated, this means that 
 * the answer had not been received by the sender cause its computation did not finish, 
 * in this case nothing will be done, no answer will be returned to the sender, when it will 
 * be available it will automatically be sent back to the sender of the original sender. 
 * </li>
 * <li>
 * the message was not received yet, it will then be dispatched into the system via the 
 * dispatcher and the answer stored as a temporary "calculation-in-progress" constant, which 
 * will lead to the previous scenario if this message will be received a second time.
 * </li>
 * </ul>
 * </p>
 * 
 * @version 0.0.1	08/dic/2009
 * @author Enrico Savioli <savioli.enrico@gmail.com>
 */
public interface MessageFilter {

	/**
	 * This method is invoked by the underlying protocol handler when a message is received 
	 * in order to check if the system already received this message and created an answer to 
	 * it and to send it back to the creator of the received message without having to 
	 * compute its answer again, or to compute it for the first time if the message had not 
	 * yet been received. 
	 * 
	 * @param message	the {@link TazioMessage} for which an answer has to be retrieved. 
	 * @return			the {@link TazioMessage} created as an answer for the message 
	 * 					passed as an input parameter.
	 */
	TazioMessage handleMessage(TazioMessage message);
	
	/**
	 * This method is used for test purposes, it accepts a {@link TazioMessage} as input 
	 * parameter and adds it to the message filter.
	 * 
	 * @param message	the {@link TazioMessage} that has to be added to the message filter.
	 */
	void add(TazioMessage message);
	
	/**
	 * This method is used for test purposes, it accepts a {@link TazioMessage} as input 
	 * parameter and removes it from the message filter.
	 * 
	 * @param message	the {@link TazioMessage} that has to be removed from the message 
	 * 					filter.
	 */
	void remove(TazioMessage message);
	
	/**
	 * This method is used for test and monitoring purposes, it simply returns the number 
	 * of elements contained into the {@link MessageFilter}. 
	 * <p>
	 * Since this object should store the answers to the already received 
	 * {@link TazioMessage}s, this method is supposed to return their number. 
	 * </p>
	 * 
	 * @return	the number of answers stored in the {@link MessageFilter}.
	 */
	int size();
}
